package com.chenwy.example.controller;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@RestController
@RequestMapping(value="file")
public class FileController {

    @RequestMapping(value="upload",consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},method = RequestMethod.POST)
    public DeferredResult<?> upload(@RequestPart(name = "file") MultipartFile multipartFile){
        System.out.println("MultipartFile=" + multipartFile);
        DeferredResult deferredResult = new DeferredResult();
        deferredResult.setResult(HttpStatus.OK);
        return deferredResult;
    }

    @RequestMapping(value="/uploadByServletFileUpload",method = RequestMethod.POST)
    public DeferredResult upload(HttpServletRequest request, HttpServletResponse response) throws FileUploadException, IOException {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        System.out.println(isMultipart);
        DeferredResult deferredResult = new DeferredResult();
        if(isMultipart){
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
            servletFileUpload.setHeaderEncoding("UTF-8");
            servletFileUpload.setSizeMax(20 * 1024 * 1024);
            List<FileItem> fileItems = servletFileUpload.parseRequest(request);
            System.out.println("文件数量="+fileItems.size());
            for(FileItem item : fileItems){
                System.out.println(item);
            }
            if(fileItems.size() > 0){
                deferredResult.setResult(HttpStatus.OK);
            }else{
                deferredResult.setResult(HttpStatus.CONFLICT);
            }
        }else{
            deferredResult.setResult(HttpStatus.BAD_REQUEST);
        }
        return deferredResult;
    }

    private static int copy(File source, File target) throws Exception{
        try (InputStream fis = new FileInputStream(source);
            OutputStream fos = new FileOutputStream(target)){
            byte[] buf = new byte[8192];
            int i;
            while ((i = fis.read(buf)) != -1) {
                fos.write(buf, 0, i);
            }
//            fos.close();
//            fis.close();
            return 1;
        }

    }

    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(1000);
    }

}
